package com.drought.rained.presenter;

import com.drought.rained.BuildConfig;
import com.drought.rained.business.CustomConsumerLJ;
import com.drought.rained.business.ParamsFactoryLJ;
import com.drought.rained.mvpview.LoginMvpViewLJ;
import com.drought.rained.response.LoginMethodResponseLJ;
import com.drought.rained.response.LoginResponseLJ;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class LoginPresenterLJ extends BasePresenterLJ {
    private LoginMvpViewLJ mvpView;

    public LoginPresenterLJ(LoginMvpViewLJ mvpView) {
        this.mvpView = mvpView;
    }

    public void getLoginMethod() {
        toSubScribe(req.getLoginMethod(BuildConfig.channel, ParamsFactoryLJ.getCommonParams(null)), new CustomConsumerLJ<LoginMethodResponseLJ>() {
            @Override
            public void call(LoginMethodResponseLJ loginMethodResponse) {
                mvpView.onGetDataNext(loginMethodResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void uploadFaceBookCode(String authCode) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("authCode", authCode);
        toSubScribe(req.uploadFaceBookCode(BuildConfig.channel, ParamsFactoryLJ.getCommonParams(paramsMap)), new CustomConsumerLJ<LoginResponseLJ>() {
            @Override
            public void call(LoginResponseLJ loginMethodResponse) {
                mvpView.onGetDataNext(loginMethodResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void requestLogin(String userName, String passWord) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("userName", userName);
        paramsMap.put("passWord", passWord);
        toSubScribe(req.login(ParamsFactoryLJ.getCommonParams(paramsMap)), new CustomConsumerLJ<LoginResponseLJ>() {
            @Override
            public void call(LoginResponseLJ loginMethodResponse) {
                mvpView.onGetDataNext(loginMethodResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

}
